package com.example.administrator.szb.fragments.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.WTContentActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.adapter.AskFragmentAdapter2;
import com.example.administrator.szb.bean.SearchBean;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/1/12.
 */

public class SearchWD extends MVPBaseFragment {
    View ll_nodata;
    ListView recyclerView;
    AskFragmentAdapter2 adapter;//回答
    private TextView top_tips;
    ArrayList<SearchBean.DataBean.ReferListBean> mdata_referListBean = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_searchs2,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(View view) {
        top_tips = view.findViewById(R.id.top_tips);
        recyclerView = view.findViewById(R.id.recyclerView);
        ll_nodata = view.findViewById(R.id.ll_nodata);
        isLoaded = true;
        recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), WTContentActivity.class);
                //SampleApplicationLike.getQueueInstance().cancelAll("tag");
                intent.putExtra("refer_id", mdata_referListBean.get(position).getId());
                startActivity(intent);
            }
        });
    }
    private boolean isLoaded = false;
    private boolean isfirst = true;
    @Override
    protected void initEvent() {
        if (!isLoaded){
            return;
        }
        if (mdata_referListBean.size()==0){
            if (!isfirst){
                recyclerView.setVisibility(View.GONE);
                ll_nodata.setVisibility(View.VISIBLE);
            }
        }else {
            ll_nodata.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
        top_tips.setVisibility(View.VISIBLE);
        top_tips.setText("共搜索到"+mdata_referListBean.size()+"条相关问答");
        if (adapter==null){
            adapter= new AskFragmentAdapter2(context,mdata_referListBean);
            recyclerView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
        isfirst = false;
    }

    @Override
    protected void initData() {

    }
    public void setDatas(List list){
        if (list==null){
            list = new ArrayList();
        }
        mdata_referListBean.clear();
        mdata_referListBean.addAll(list);
        initEvent();
    }


}
